package com.woniuxy.carlife.carlifeuserweb.service.impl;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sun.javaws.exceptions.CacheAccessException;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifecommonsweb.util.RandomCode;
import com.woniuxy.carlife.carlifeusermodel.param.UserAccount;

import com.woniuxy.carlife.carlifeusermodel.param.UserTelephone;
import com.woniuxy.carlife.carlifeuserweb.entity.User;
import com.woniuxy.carlife.carlifeuserweb.entity.UserInfo;
import com.woniuxy.carlife.carlifeuserweb.mapper.UserMapper;
import com.woniuxy.carlife.carlifeuserweb.service.StatisticsService;
import com.woniuxy.carlife.carlifeuserweb.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zjq
 * @ClassName:UserServiceImpl
 * @Description:
 * @date 2021/4/6 20:02
 **/

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;




    /*
     * 功能描述〈查询手机号是否存在〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/7 22:20
     * @throws
     */

    @Override
    public boolean findPnone(String userPhone) {
        if (ObjectUtil.isNotNull(userPhone)) {
            // 查询条件
            QueryWrapper<User> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_phone", userPhone).eq("user_status", 1);
//        // 开始查询


            User usern = userMapper.selectOne(queryWrapper);
            if (ObjectUtil.isNotNull(usern)) {

                return true;
            }
        }
        return false;
    }


    /*
     * 功能描述〈查询账号〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/7 22:31
     * @throws
     */

    @Override
    public boolean findAccount(String useraccount) {
        if (ObjectUtil.isNotNull(useraccount)) {
            // 查询条件
            QueryWrapper<User> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_account", useraccount).eq("user_status", 1);
//        // 开始查询
            User usern = userMapper.selectOne(queryWrapper);
            if (ObjectUtil.isNotNull(usern)) {
                return true;
            }
        }
        return false;
    }

    /*
     * 功能描述〈账号登录〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/7 22:31
     * @throws
     */

    @Override
    public User accountLogin(UserAccount userAccount) {
        if (ObjectUtil.isNotNull(userAccount.getUseraccount()) && ObjectUtil.isNotNull(userAccount.getUserPassword())) {
            // 查询条件
            QueryWrapper<User> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_account", userAccount.getUseraccount()).eq("user_status", 1);
//        // 开始查询

            User usern = userMapper.selectOne(queryWrapper);

            if (ObjectUtil.isNotNull(usern)) {
                Md5Hash md5Hash = new Md5Hash(userAccount.getUserPassword(), usern.getUserSalt(), 1024);
                String md5Password = md5Hash.toHex();

                if (usern.getUserPassword().equals(md5Password)) {
                    return usern;
                }
            }
            return null;
        }
        return null;
    }
    /*
     * 功能描述〈账号密码注册〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/8 15:08
     * @throws
     */

    @Override
    public User accountRegister(UserAccount userAccount) {
        boolean account = findAccount(userAccount.getUseraccount());
        if (account) {
            return null;
        }
        User one = CopyUtil.getOne(userAccount, User.class);
        String salt = RandomCode.getCode(8);
        Md5Hash md5Hash = new Md5Hash(one.getUserPassword(), salt, 1024);
        String md5Password = md5Hash.toHex();
        one.setUserAccount(userAccount.getUseraccount());
        one.setUserSalt(salt);
        one.setUserPassword(md5Password);
        one.setUserStatus(1);
        int insert = userMapper.insert(one);
        if (insert > 0) {


            return one;
        }
        return null;
    }

    /*
     * 功能描述〈手机号密码注册〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/8 15:07
     * @throws
     */

    @Override
    public User telephoneRegister(UserTelephone userTelephone) {
        boolean pnone = findPnone(userTelephone.getUserPhone());

        if (pnone) {
            return null;
        }

        User one = CopyUtil.getOne(userTelephone, User.class);
        String salt = RandomCode.getCode(8);
        Md5Hash md5Hash = new Md5Hash(one.getUserPassword(), salt, 1024);
        String md5Password = md5Hash.toHex();
        one.setUserPhone(userTelephone.getUserPhone());
        one.setUserSalt(salt);
        one.setUserPassword(md5Password);
        one.setUserAccount(userTelephone.getUserPhone());

//        查询账号是否存在
        boolean account = findAccount(one.getUserAccount());
        if (!account) {
            one.setUserStatus(1);
            int insert = userMapper.insert(one);
            if (insert > 0) {

                return one;
            }
        }
        return null;
    }


    /*
     * 功能描述〈手机号验证码注册〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/8 15:07
     * @throws
     *
     */
    @Override
    public User phoneRegister(UserTelephone userTelephone) {
        boolean pnone = findPnone(userTelephone.getUserPhone());

        if (pnone) {
            return null;
        }

        User one = CopyUtil.getOne(userTelephone, User.class);

        one.setUserPhone(userTelephone.getUserPhone());


        one.setUserAccount(userTelephone.getUserPhone());

//        查询账号是否存在
        boolean account = findAccount(one.getUserAccount());
        if (!account) {
            one.setUserStatus(1);
            int insert = userMapper.insert(one);
            if (insert > 0) {

                return one;
            }
        }
        return null;
    }

    /*
     * 功能描述〈手机号登录〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/7 22:31
     * @throws
     */

    @Override
    public User phoneLogin(UserTelephone userTelephone) {
        if (ObjectUtil.isNotNull(userTelephone.getUserPhone())) {
            // 查询条件
            QueryWrapper<User> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_phone", userTelephone.getUserPhone()).eq("user_status", 1);
//        // 开始查询
            User usern = userMapper.selectOne(queryWrapper);
            if (ObjectUtil.isNotNull(usern)) {
                return usern;
            }
        }
        return null;
    }




    /*
     * 功能描述〈用户修改〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 11:32
     * @throws
     */

    @Override
    public Integer modifyUserById(User user) {

        if (ObjectUtil.isNull(user)) {
            return null;
        }

        UpdateWrapper<User> wrapper = new UpdateWrapper<>();

        wrapper.eq("user_id", user.getUserId()).eq("user_status", 1);
        int update = userMapper.update(user, wrapper);

        if (update > 0) {
            return 1;
        }
        return 0;

    }


    /*
     * 功能描述〈用户删除（逻辑删除）〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 12:07
     * @throws
     */

    @Override
    public Integer deleteUserByUserId(User user) {
        if (ObjectUtil.isNull(user)) {
            return null;
        }

        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        user.setUserStatus(0);
        wrapper.eq("user_id", user.getUserId()).eq("user_status", 1);
        int update = userMapper.update(user, wrapper);

        if (update > 0) {
            return 1;
        }
        return 0;
    }
}
